package com.estun.backroundManagementService.modules.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.estun.backroundManagementService.modules.product.mapper.ProductMapper;
import com.estun.backroundManagementService.modules.product.mapper.ProductStationTimeMapper;
import com.estun.backroundManagementService.modules.product.model.ProductStationTime;
import com.estun.backroundManagementService.modules.product.service.ProductPassStationService;
import com.estun.backroundManagementService.modules.product.service.ProductStationTimeService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 产品过站记录表 服务实现类
 * </p>
 *
 * @author machine
 * @since 2023-02-14
 */
@Service
@AllArgsConstructor
@Slf4j
public class ProductStationTimeServiceImpl extends ServiceImpl<ProductStationTimeMapper, ProductStationTime> implements ProductStationTimeService {


    private final ProductPassStationService productPassStationService;

    @Override
    public ProductStationTime getByProductIdAndStationId(Long productId, String stationId) {
        List<ProductStationTime> list = list(new QueryWrapper<ProductStationTime>().eq("station_id", stationId)
                .eq("product_id", productId));
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;

    }

    @Override
    public List<ProductStationTime> getByProductId(Long id) {
        return list(new QueryWrapper<ProductStationTime>().eq("product_id", id));
    }

    /**
     * 获取待推送的站点数据 状态为待推送 且 已过站的数据（diff_second不为0，判断为已过站)
     */
    @Override
    public List<ProductStationTime> getListToBePushed() {
        return list(new QueryWrapper<ProductStationTime>()
                .ne("submit_status", 1)
                .ne("diff_second", 0));
    }

    @Override
    public void handleNoProductNoByProductId(Long productId, String stationId) {
        ProductStationTime byProductIdAndStationId = getByProductIdAndStationId(productId, stationId);
        if (byProductIdAndStationId != null) {
            UpdateWrapper<ProductStationTime> updateWrapper = new UpdateWrapper<>();
            Integer submitNumber = byProductIdAndStationId.getSubmitNumber();
            if (submitNumber < 10) {
                log.error("【过站记录推送】productId:{},第{}次推送没有产品码", productId, submitNumber);
                updateWrapper
                        .eq("product_id", productId)
                        .eq("station_id", stationId)
                        .set("submit_number", submitNumber + 1);
            } else {
                updateWrapper
                        .eq("product_id", productId)
                        .eq("station_id", stationId)
                        .set("submit_status", 1)
                        .set("submit_number", 999);
            }
            update(updateWrapper);
        }
    }

    @Override
    public ProductStationTime getByProductNoAndStationCode(String productNo, String stationCode) {


        return null;
    }
}
